
#include "incs.h"
#include "CEGUIOwner.h"
#include <Ogre.h>


//CEGUI::OgreCEGUIRenderer * CEGUIOwner::mGUIRenderer = 0;
//
//CEGUI::System * CEGUIOwner::mGUISystem = 0;


CEGUIOwner::CEGUIOwner() : mGUIRenderer(0), mGUISystem(0)
{

}

CEGUI::OgreCEGUIRenderer * CEGUIOwner::GetGUIRenderer()
{
	return mGUIRenderer;
}

CEGUI::System * CEGUIOwner::GetGUISystem()
{
	return mGUISystem;
}

void CEGUIOwner::InitializeCEGUI( Ogre::RenderWindow * win, Ogre::SceneManager * mgr )
{
	try
	{
		// We make sure we have no renderer yet. Should never happen.
		if(mGUIRenderer)
			throw std::runtime_error("Renderer exists already.");

		// We create the renderer, which will draw the widgets through Ogre.
		mGUIRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, 
			false, 3000, mgr);


		// Check we have no CEGUI::System yet. Should never happen either.
		if(mGUISystem)
			throw std::runtime_error("CEGUI::System exists already.");

		// We create the CEGUI system itself, which will use the Ogre
		// renderer to draw its widgets.
		mGUISystem = new CEGUI::System(mGUIRenderer);

		// We load the schemes we need.
		CEGUI::SchemeManager::getSingleton().loadScheme(
			(CEGUI::utf8*)"TaharezLookSkin.scheme");
		//CEGUI::SchemeManager::getSingleton().loadScheme(
		//	(CEGUI::utf8*)"VanillaSkin.scheme");
		//CEGUI::SchemeManager::getSingleton().loadScheme(
		//	(CEGUI::utf8*)"WindowsLook.scheme");

		// Define the default mouse cursor.
		mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", 
			(CEGUI::utf8*)"MouseArrow");

		// Define the default font.
		mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");

		// Activate the mouse cursor. 
		CEGUI::MouseCursor::getSingleton().setImage(
			CEGUI::System::getSingleton().getDefaultMouseCursor());
	}
	catch(...)
	{
		throw Ogre::Exception(50, std::string("CEGUIOwner::InitializeCEGUI"), 
			std::string("CEGUIOwner::InitializeCEGUI"));
	}
}

CEGUIOwner::~CEGUIOwner()
{
	//CleanupCEGUI();
}

void CEGUIOwner::CleanCEGUI()
{
	delete mGUISystem;
	mGUISystem = 0;

	delete mGUIRenderer;
	mGUIRenderer = 0;

	//CEGUI::SchemeManager * sm = CEGUI::SchemeManager::getSingletonPtr();
	//if(sm)
	//	sm->unloadAllSchemes();
	//delete mGUIRenderer;
	//delete mGUISystem;
	//mGUIRenderer = 0;
	//mGUISystem = 0;

	//mGUIRenderer->clearRenderList();
	//mGUISystem->getRenderer()->clearRenderList();
	//mGUIRenderer->destroyAllTextures();
	//mGUISystem->setGUISheet(0);
	//mGUIRenderer->setTargetSceneManager(0);

}

void CEGUIOwner::ActivateMouseCursor( bool activar )
{
	CEGUI::MouseCursor * mouse = CEGUI::MouseCursor::getSingletonPtr();
	if(!mouse)
		return;

	if(activar)
		mouse->show();
	else
		mouse->hide();
}